package com.ruoyi.web.controller.pc;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.MyArticle;
import com.ruoyi.web.service.MyArticleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* @Project: serve
* @Package com.ruoyi.web.controller
* @Description: TODO(我的文章Controller)
* @author : GuitarXiaohe
* @date Date : 2023年10月26日 14:36
* @version V1.0
*/

@RestController
@RequestMapping("/web/article")
public class MyArticleController  extends BaseController {
    @Resource
    private MyArticleService myArticleService;

    /**
     * 查询我的文章列表
     */
    @PreAuthorize("@ss.hasPermi('web:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyArticle myArticle)
    {
        startPage();
        List<MyArticle> list = myArticleService.selectMyArticleList(myArticle);
        return getDataTable(list);
    }

    /**
     * 导出我的文章列表
     */
    @PreAuthorize("@ss.hasPermi('web:article:export')")
    @Log(title = "我的文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MyArticle myArticle)
    {
        List<MyArticle> list = myArticleService.selectMyArticleList(myArticle);
        ExcelUtil<MyArticle> util = new ExcelUtil<MyArticle>(MyArticle.class);
        util.exportExcel(response, list, "我的文章数据");
    }

    /**
     * 获取我的文章详细信息
     */
    @PreAuthorize("@ss.hasPermi('web:article:query')")
    @GetMapping(value = "/{articleId}")
    public AjaxResult getInfo(@PathVariable("articleId") Long articleId)
    {
        return AjaxResult.success(myArticleService.selectMyArticleByArticleId(articleId));
    }

    /**
     * 新增我的文章
     */
    @PreAuthorize("@ss.hasPermi('web:article:add')")
    @Log(title = "我的文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyArticle myArticle)
    {
        return toAjax(myArticleService.insertMyArticle(myArticle));
    }

    /**
     * 修改我的文章
     */
    @PreAuthorize("@ss.hasPermi('web:article:edit')")
    @Log(title = "我的文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyArticle myArticle)
    {
        return toAjax(myArticleService.updateMyArticle(myArticle));
    }

    /**
     * 删除我的文章
     */
    @PreAuthorize("@ss.hasPermi('web:article:remove')")
    @Log(title = "我的文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/{articleIds}")
    public AjaxResult remove(@PathVariable Long[] articleIds)
    {
        return toAjax(myArticleService.deleteMyArticleByArticleIds(articleIds));
    }
}